package com.lixw.saa.controller;

import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @ClassName: BailianRagController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/10/5 10:07
 **/
@RestController
public class BaiLianRagController {
    @Resource
    private ChatClient chatClient;
    @Resource
    private DashScopeApi dashScopeApi;

    @GetMapping("/bailian/rag/chat")
    public Flux<String> chat(@RequestParam(value = "msg",defaultValue = "00000错误编码什么意思?") String msg) {
        //1. 百炼平台文档检索器设置参数选项
        DashScopeDocumentRetrieverOptions documentRetrieverOptions = DashScopeDocumentRetrieverOptions
                .builder().withIndexName("ops").build();
        //2.  百炼平台文档检索器
        DashScopeDocumentRetriever dashScopeDocumentRetriever = new DashScopeDocumentRetriever(dashScopeApi, documentRetrieverOptions);
        return chatClient.prompt()
                .user(msg)
                .advisors(new DocumentRetrievalAdvisor(dashScopeDocumentRetriever))
                .stream()
                .content();
    }
}